<?php

/**
 * contract_type actions.
 *
 * @package    erp-dnict
 * @subpackage contract_type
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class contract_typeActions extends sfActions
{
 /**
  * Executes index actions
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $q = Doctrine_Query::create()
            ->from('ContractType ct')
            ->addWhere('ct.is_active = ?', true);

    $this->contract_typies = $q->execute();
    return sfView::SUCCESS;
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->contract_type_id = $request->getParameter('id', null);
    $this->form = new ContractTypeForm();

    if ($this->contract_type_id) {
      $contract_type = Doctrine_Core::getTable('ContractType')->find($this->contract_type_id);
      $this->form = new ContractTypeForm($contract_type);
    }

    if ($request->isMethod('post')) {
      $form_data = $request->getParameter('contract_type');
      $this->form->bind($form_data);
      if ($this->form->isValid()) {
        if (!$request->getParameter('id'))
          $contract_type = $this->form->save();
        else { // update
          $contract_type = Doctrine_Core::getTable('ContractType')->find($request->getParameter('id'));
          if ($contract_type) {
            $contract_type->name = $form_data['name'];
            $contract_type->duration = $form_data['duration'];
            $contract_type->description = $form_data['description'];
            $contract_type->is_active = $form_data['is_active'] == 'on' ? true : false;
            $contract_type->save();
          }
        }
        $this->redirect('@contract_type_index');
      }
    }
    return sfView::SUCCESS;
  }

  public function executeRemove(sfWebRequest $request)
  {
    $contract_type_id = $request->getParameter('id', null);
    if ($contract_type_id) {
      $contract_type = Doctrine_Core::getTable('ContractType')->find($contract_type_id);
      if ($contract_type) {
        foreach ($contract_type->Profiles as $profile) {
          $profile->contract_type_id = 0;
          $profile->save();
        }
        $contract_type->is_active = false;
        $contract_type->save();
        $this->redirect('@contract_type_index');
      }
    }
  }
}
